<!--
 * @Description:
 * @Author: zhaolei
 * @Date: 2023-04-11 11:12:29
-->
<template>
  <el-dialog
    title="批量替换推荐商品"
    :visible.sync="visible"
    width="60%"
    append-to-body
  >
    <div style="margin-bottom: 20px">
      <div style="color: #409eff">
        注意：若所选宠物对应宠物用品分类下已有商品信息，批量推荐商品操作会直接全部替换更新掉
      </div>
      <div>
        若宠物A已配置【它的主粮】商品，则批量推荐商品直接替换宠物A【它的主粮】为新的商品信息
      </div>
    </div>
    <el-form
      ref="form"
      :model="form"
    >
      <el-form-item
        label="宠物："
        prop="selectedPet"
        :rules="[{ required: true, message: '请选择宠物', trigger: 'blur' }]"
      >
        <el-select
          v-model="form.selectedPet"
          multiple
          filterable
          placeholder="请选择宠物"
          style="width: 500px"
        >
          <el-option
            v-for="item in petList"
            :key="item.id"
            :label="item.code + '(' + item.nickName + ')'"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <like-goods
      ref="likeGoods"
      @loading="(val) => (loading = val)"
      @success="submitSuccess"
    ></like-goods>
    <div class="flex-end">
      <el-button @click="visible = false">取消</el-button>
      <el-button
        class="btn"
        :loading="loading"
        type="primary"
        @click="confirm"
      >
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import LikeGoods from './likeGoods.vue';
import { selectAdoptPet } from '@/api/core/agreement';

export default {
  name: 'BatchRecommendPopup',
  components: { LikeGoods },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      petList: [],
      form: {
        selectedPet: [],
      },
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      },
    },
  },
  watch: {
    visible(val) {
      if (!val) {
        // 清除数据
        this.$refs.likeGoods.resetGoods();
        this.clearData();
      } else {
        this.fetchPetList();
      }
    },
  },
  methods: {
    submitSuccess() {
      this.$message.success('操作成功！');
      this.visible = false;
    },
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$refs.likeGoods.submit(this.form.selectedPet);
        }
      });
    },
    clearData() {
      this.form.selectedPet = [];
    },
    async fetchPetList() {
      const { data } = await selectAdoptPet();
      this.petList = data.data;
      console.log('petList', this.petList);
    },
  },
};
</script>

<style lang="scss" scoped></style>
